package com.bdqn.qnnews.article.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdqn.qnnews.articel.api.dto.ApAuthorDto;
import com.bdqn.qnnews.article.convert.ApAuthorConvert;
import com.bdqn.qnnews.article.entity.ApAuthor;
import com.bdqn.qnnews.article.service.ApAuthorService;
import com.bdqn.qnnews.common.base.R;
import com.bdqn.qnnews.rest.exception.ApplicationException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author xlz
 * @since 2023-12-26
 * @description APP文章作者信息表
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1")
@Tag(name = "APP文章作者信息表控制器类",
        description = "ApAuthorController APP文章作者信息表 后端数据接口")
public class ApAuthorController {

    private final ApAuthorService apAuthorService;

    /**
     * 通过id查询 APP文章作者信息表
     * @param userId app端用户id
     * @return Result
     */
    @Operation(summary = "通过id查询", description = "通过app端用户id查询")
    @GetMapping("/author")
    public R<ApAuthorDto> getByApUserId(@RequestParam("apUserId") Long userId) {
        if (userId == null){
            throw new ApplicationException("查询的用户id为空");
        }
        final LambdaQueryWrapper<ApAuthor> wrapper =
                Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId);

        // 查询文章作者信息
        final ApAuthor apAuthor = apAuthorService.getOne(wrapper);
        // 将查询到的作者entity转换为dto
        final ApAuthorDto apAuthorDto = ApAuthorConvert.INSTANCE.entityToDto(apAuthor);

        return R.ok(apAuthorDto);
    }

    /**
     * 新增 APP文章作者信息表
     * @param apAuthorDto APP用户微服务传入的用户信息
     * @return Result
     */
    @Operation(summary = "新增APP文章作者信息表", description = "新增APP文章作者信息表")
    @PostMapping("/author")
    public R<String> save(@RequestBody ApAuthorDto apAuthorDto) {
        // 将接收到的dto转换为entity，再去操作数据保存
        final ApAuthor apAuthor = ApAuthorConvert.INSTANCE.dtoToEntity(apAuthorDto);
        apAuthorService.save(apAuthor);
        return R.ok("保存作者成功");
    }

    /**
     * 修改 APP文章作者信息表
     * @param apAuthor APP文章作者信息表
     * @return Result
     */
    @Operation(summary = "修改APP文章作者信息表", description = "修改APP文章作者信息表")
    @PutMapping("")
    public R<String> updateById(@RequestBody ApAuthor apAuthor) {
        return R.ok();
    }

    /**
     * 通过id删除 APP文章作者信息表
     * @param id id
     * @return Result
     */
    @Operation(summary = "通过id删除APP文章作者信息表",
               description = "通过id删除APP文章作者信息表")
    @DeleteMapping("/{id}")
    public R<String> removeById(@PathVariable Long id) {
        return R.ok();
    }

}
